import { useTokenStore } from "@/stores/mytoken";
import request from "@/utils/request";

type LoginInfo = {
  phone : string,
  code? : string,
  password : string,
}

type LoginResult = {
  success : boolean,
  state : number,
  message : string,
  content : string,
}

type UserInfo = {
  success : boolean,
  message : string,
  state : number,
  content : {
    isUPdatedPassword: boolean,
    portrait: string,
    userName:string
  }
}

// 用户请求登录
export const login = (loginInfo:LoginInfo) => {
  return request<LoginResult>({
    url: "/api/user/login",
    method: "post",
    data:`phone=${loginInfo.phone}&password=${loginInfo.password}`,
  });
}

export const getInfo = () => {
  return request<UserInfo>({
    url: "/api/user/info",
    method: "get",
  });
}

// 用户退出
export const logout = () => {
  return request({
    url: "/api/user/logout",
    method: "post",
  })
}

type RToken = {
  message: string,
  state: number,
  success:boolean
  content:string
}

// let promiseRT: Promise<any>
// let isRefreshing = false

// 刷新token
export const refreshToken = () => {
  // if (isRefreshing) {
    // return promiseRT
  // }

  // isRefreshing = true
  // promiseRT = request<RToken>({
  //   url: "/api/user/refresh_token",
  //   method: "post",
  //   params: {
  //     refreshtoken: useTokenStore().token?.refresh_token
  //   }
  // }).finally(() => {
  //   isRefreshing = false
  // })

  // return promiseRT

  return request<RToken>({
    url: "/api/user/refresh_token",
    method: "post",
    params: {
      refreshtoken: useTokenStore().token?.refresh_token
    }
  })
}